package com.example.repair

import android.os.Bundle
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.RepairEntityItem
import com.example.base.mvi.BaseActivity
import com.example.base.mvi.HttpType
import com.example.base.mvi.UiState
import com.example.repair.databinding.ActivityRepairBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/repair/RepairActivity")
@AndroidEntryPoint
class RepairActivity : BaseActivity<ActivityRepairBinding,RepairIntent,RepairViewModel>() {
    val repairAdapter by lazy { RepairAdapter() }
    override fun getLayout(): Int = R.layout.activity_repair

    override fun getVM(): Class<RepairViewModel>  = RepairViewModel::class.java

    override fun initView() {
        binding.head.apply {
            headBack.setOnClickListener {
                finish()
            }
            headTitle.text = "报修管理"
            headAdd.setOnClickListener {
                ARouter.getInstance().build("/repair/RepairEditActivity").navigation()
            }
        }
        binding.rvRepair.apply {
            adapter = repairAdapter
            layoutManager = LinearLayoutManager(this@RepairActivity)
        }

        repairAdapter.addChildClickViewIds(R.id.repair_update)
        repairAdapter.setOnItemChildClickListener { adapter, view, position ->
            //判断当前的状态
            val status = repairAdapter.data[position].processingStatus
            when(status){
                "处理中"->{

                    //点击审核完成，，页面上状态变成已完成  背景色变成绿色 同时审核完成的控件消失
                    repairAdapter.data[position].processingStatus = "已完成"
                    (repairAdapter.getViewByPosition(position,R.id.repair_status) as TextView).text = "已完成"
                    (repairAdapter.getViewByPosition(position,R.id.repair_status) as TextView).setBackgroundResource(R.drawable.bg_green)
                    (repairAdapter.getViewByPosition(position,R.id.repair_update) as TextView).isVisible = false
                    viewModel.sendIntent(RepairIntent.repairUpdare(repairAdapter.data[position].id,repairAdapter.data[position]))
                }
                "待处理"->{
                    //弹出一个单选对话框
                    val items = arrayOf("张师傅","李师傅","王师傅")
                    AlertDialog.Builder(this)
                        .setTitle("维修人员指派")
                        .setSingleChoiceItems(items,0){
                            a,b->
                        }
                        .setNegativeButton("取消"){
                            a,b->
                        }
                        .setPositiveButton("确定指派"){
                            a,b->
                            repairAdapter.data[position].processingStatus = "处理中"
                            (repairAdapter.getViewByPosition(position,R.id.repair_status) as TextView).text = "处理中"
                            (repairAdapter.getViewByPosition(position,R.id.repair_status) as TextView).setBackgroundResource(R.drawable.bg_yellow)
                            (repairAdapter.getViewByPosition(position,R.id.repair_update) as TextView).text = "审核完成"
                            viewModel.sendIntent(RepairIntent.repairUpdare(repairAdapter.data[position].id,repairAdapter.data[position]))
                        }
                        .show()

                }
            }
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            viewModel.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        when (it.type) {
                            HttpType.DEFAULT -> {
                                val items = it.data as Collection<RepairEntityItem>
                                repairAdapter.apply {
                                    data.clear()
                                    addData(items.sortedByDescending { it.id })
                                    notifyDataSetChanged()
                                }
                            }
                            HttpType.UPDATE->{
                                ToastUtils.showLong("更新报修成功")
                            }
                            else -> {}
                        }

                    }

                    else -> {}
                }
            }
        }
    }
    override fun onResume() {
        super.onResume()
        viewModel.sendIntent(RepairIntent.repair)
    }

}